package com.neo.sevice.impl;

import com.neo.dao.SysPermissionDao;
import com.neo.dao.SysRoleDao;
import com.neo.dao.UserInfoDao;
import com.neo.entity.SysPermission;
import com.neo.entity.SysRole;
import com.neo.entity.UserInfo;
import com.neo.sevice.UserInfoService;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.transaction.Transactional;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements UserInfoService {
	
	@Autowired
    private UserInfoDao userInfoDao;
    
    @Autowired
	private SysRoleDao sysRoleDao;
	
	@Autowired
	private SysPermissionDao sysPermissionDao;
    
//	@PersistenceContext
//	private EntityManager entityManager;
	
    @Override
    public UserInfo findByUsername(String username) {
        System.out.println("UserInfoServiceImpl.findByUsername()");
        return userInfoDao.findByUsername(username);
    }
    
    @Override
    public List<UserInfo> findAllUserInfo() {
    	List<UserInfo> list = userInfoDao.findAll();
    	System.out.println(list);
    	return list;
    }

    @Transactional
	@Override
	public void initLoginData() {
		String hashAlgorithmName = "MD5";
		Object credentials = "12345678";
		Object salt1 = ByteSource.Util.bytes("123456admin");
		Object salt2 = ByteSource.Util.bytes("123456junson");
		int hashIterations = 2;
		
		Object password1 = new SimpleHash(hashAlgorithmName, credentials, salt1, hashIterations);
		System.out.println(password1.toString());
		Object password2 = new SimpleHash(hashAlgorithmName, credentials, salt2, hashIterations);
		System.out.println(password2.toString());
		
		UserInfo adminU = new UserInfo(null, "admin", "管理员", password1.toString(), "123456", (byte) 0);
		UserInfo junsonU = new UserInfo(null, "junson", "会员", password2.toString(), "123456", (byte) 0);
		
		SysRole adminR = new SysRole("admin", "管理员", false);
		SysRole vipR = new SysRole("vip", "vip会员", false);
		
		SysPermission view = new SysPermission("用户管理", "menu", "userInfo/userList", "userInfo:view", 0L, "0/", false);
		SysPermission add = new SysPermission("用户添加", "button", "userInfo/userAdd", "userInfo:add", 1L, "0/1", false);
		SysPermission del = new SysPermission("用户删除", "button", "userInfo/userDel", "userInfo:del", 1L, "0/1", false);
		SysPermission update = new SysPermission("用户修改", "button", "userInfo/userUpdate", "userInfo:update", 1L, "0/1", false);
		
		Set<SysRole> roleList = new HashSet<>();
		roleList.add(adminR);
		roleList.add(vipR);
		adminU.setRoleSet(roleList);
		
		Set<SysRole> roleList2 = new HashSet<>();
		roleList2.add(vipR);
		junsonU.setRoleSet(roleList2);
		//------------------------------------------
		
		Set<UserInfo> userInfoslist = new HashSet<>();
		userInfoslist.add(adminU);
		adminR.setUserInfos(userInfoslist);
		
		Set<UserInfo> userInfoslist2 = new HashSet<>();
		userInfoslist2.add(adminU);
		userInfoslist2.add(junsonU);
		vipR.setUserInfos(userInfoslist2);
		
		Set<SysPermission> sysPermissionslist = new HashSet<>();
		sysPermissionslist.add(view);
		sysPermissionslist.add(add);
		sysPermissionslist.add(del);
		sysPermissionslist.add(update);
		adminR.setPermissions(sysPermissionslist);
		
		Set<SysPermission> sysPermissionslist2 = new HashSet<>();
		sysPermissionslist2.add(view);
		sysPermissionslist2.add(add);
		sysPermissionslist2.add(update);
		vipR.setPermissions(sysPermissionslist2);
		//----------------------------------------------------
		
		Set<SysRole> roleList3 = new HashSet<>();
		roleList3.add(adminR);
		roleList3.add(vipR);
		view.setRoles(roleList3);
		add.setRoles(roleList3);
		update.setRoles(roleList3);
		
		Set<SysRole> roleList4 = new HashSet<>();
		roleList4.add(adminR);
		del.setRoles(roleList4);
		/*entityManager.persist(adminU);
		entityManager.persist(junsonU);
		entityManager.persist(adminR);
		entityManager.persist(adminR);
		entityManager.persist(view);
		entityManager.persist(add);
		entityManager.persist(del);
		entityManager.persist(update);*/
		
		userInfoDao.save(adminU);
		userInfoDao.save(junsonU);
		sysRoleDao.save(adminR);
		sysRoleDao.save(vipR);
		sysPermissionDao.save(view);
		sysPermissionDao.save(add);
		sysPermissionDao.save(del);
		sysPermissionDao.save(update);
		
	}
    
}